<!-- 打印 -->
<template><div>
  <div v-if="'preview' === this.componentType">
    <div style="display: none;">
      <xpaas-test-book-cover :printId="singleCourseInfo.id_sjcfm" :courseInfo="singleCourseInfo" v-if="'sjcfm' === this.printType" />
      <xpaas-assessment-implementation-plan :printId="singleCourseInfo.id_kckh" :courseInfo="singleCourseInfo" v-if="'kckh' === this.printType" />
      <xpaas-proposition-plan :printId="singleCourseInfo.id_mtjh" :courseInfo="singleCourseInfo" v-if="'mtjh' === this.printType" />
      <xpaas-examination-room-registration-form :printId="singleCourseInfo.id_kcdjb" :courseInfo="singleCourseInfo" v-if="'kcdjb' === this.printType" />
      <xpaas-delivery-of-summons :printId="singleCourseInfo.id_sycp" :courseInfo="singleCourseInfo" v-if="'sycp' === this.printType" />
      <xpaas-test-paper-bag-cover useType="batch" :printId="singleCourseInfo.id_sjdfp" :courseInfo="singleCourseInfo" v-if="'sjdfp' === this.printType" />
      <xpaas-test-paper-analysis-form :printId="singleCourseInfo.id_ksfxb" :courseInfo="singleCourseInfo" v-if="'ksfxb' === this.printType" />
      <xpaas-general-evaluation-report :printId="singleCourseInfo.id_zpcjd" :courseInfo="singleCourseInfo" v-if="'zpcjd' === this.printType" />
    </div>
  <el-button size="mini" type="primary" @click="printSingle">预览</el-button>
  </div><div v-else>
    <el-button size="mini" type="success" @click="printAll">打印</el-button>
    <div style="display: none;">
    <div id="xpaas-print-all-selected-test-files">
      <xpaas-test-book-cover :courseInfo="i" v-for="i in checkedPapers.sjcfm" :key="i.id" />
      <xpaas-assessment-implementation-plan :courseInfo="i" v-for="i in checkedPapers.kckh" :key="i.id" />
      <xpaas-proposition-plan :courseInfo="i" v-for="i in checkedPapers.mtjh" :key="i.id" />
      <xpaas-examination-room-registration-form :courseInfo="i" v-for="i in checkedPapers.kcdjb" :key="i.id" />
      <xpaas-delivery-of-summons :courseInfo="i" v-for="i in checkedPapers.sycp" :key="i.id" />
      <xpaas-test-paper-bag-cover useType="single" :courseInfo="i" v-for="i in checkedPapers.sjdfp2" :key="i.id" />
      <xpaas-test-paper-analysis-form :courseInfo="i" v-for="i in checkedPapers.ksfxb" :key="i.id" />
      <xpaas-general-evaluation-report :courseInfo="i" v-for="i in checkedPapers.zpcjd" :key="i.id" />
    </div>
    </div>
  </div>
</div></template>
<script>
import TestBookCover from "./TestBookCover.vue"; // 试卷册封面
import AssessmentImplementationPlan from "./AssessmentImplementationPlan.vue"; // 课程考核实施计划
import PropositionPlan from "./PropositionPlan.vue"; // 课程考核实施计划
import ExaminationRoomRegistrationForm from "./ExaminationRoomRegistrationForm.vue"; // 考场登记表
import DeliveryOfSummons from "./DeliveryOfSummons.vue"; // 送印传票
import TestPaperBagCover from "./TestPaperBagCover.vue"; // 试卷袋封皮
import TestPaperAnalysisForm from "./TestPaperAnalysisForm.vue"; // 试卷分析表
import GeneralEvaluationReport from "./GeneralEvaluationReport.vue"; // 总评成绩单
import print from '../../../../../public/cdn/print/print.min';

export default {
  components: {
    "xpaas-test-book-cover": TestBookCover,
    "xpaas-assessment-implementation-plan": AssessmentImplementationPlan,
    "xpaas-proposition-plan": PropositionPlan,
    "xpaas-examination-room-registration-form": ExaminationRoomRegistrationForm,
    "xpaas-delivery-of-summons": DeliveryOfSummons,
    "xpaas-test-paper-bag-cover": TestPaperBagCover,
    "xpaas-test-paper-analysis-form": TestPaperAnalysisForm,
    "xpaas-general-evaluation-report": GeneralEvaluationReport
  },
  props: {
    singleCourseInfo: {type: Object},
    printType: {type: String},
    componentType: {type: String},
    checkedPapers: {
      checkedTotal: 0,
      sjcfm: [],
      kckh: [],
      mtjh: [],
      kcdjb: [],
      sycp: [],
      sjdfp: [],
      sjdfp2: [],
      ksfxb: [],
      zpcjd: [],
      abj: [],
      da: [],
      dtj: []
    }
  },
  data() {
    return {
      readyForPrint: 0
    }
  },
  methods: {
    printCallback: function(pCheckedPapers) {
      console.log("pCheckedPapers =>", pCheckedPapers);
      this.readyForPrint = 2;
      let targetArray = [];
      for (const x of pCheckedPapers.sjdfp) {
        for (let y=0; y<x.sexamination.gjd; y++) {
						const newCourseInfo = {
							xueqi_1: x.xueqi_1,
							xueqi_2: x.xueqi_2,
							sexamination: {
								courseid: x.sexamination.courseid,
								coursename: x.sexamination.coursename,
								createUserName: x.sexamination.createUserName,
								deptid: x.sexamination.deptid,
								deptname: x.sexamination.deptname,
								djd: (y + 1),
								dsf: x.sexamination.dsf,
								gjd: x.sexamination.gjd,
								gradeid: x.sexamination.gradeid,
								gradename: x.sexamination.gradename,
								id: x.sexamination.id,
								jrsid: x.sexamination.jrsid,
								jsrname: x.sexamination.jsrname,
								jxdw: x.sexamination.jxdw,
								qkdsr: x.sexamination.qkdsr,
								skdsr: x.sexamination.skdsr,
								specialty: x.sexamination.specialty,
								specialtyid: x.sexamination.specialtyid,
								teacher: x.sexamination.teacher,
								teacherid: x.sexamination.teacherid,
								termid: x.sexamination.termid,
								termname: x.sexamination.termname,
								testpapperandprintid: x.sexamination.testpapperandprintid,
								updateUserName: x.sexamination.updateUserName,
								ykdsr: x.sexamination.ykdsr
							}
						};
            targetArray.push(newCourseInfo);
        }
      }
      pCheckedPapers.sjdfp2 = targetArray;
      this.checkedPapers = pCheckedPapers;
      console.log("checkedPapers =>", this.checkedPapers);
    },
    printAll: function() {
      this.checkedPapers = {
        checkedTotal: 0,
        sjcfm: [],
        kckh: [],
        mtjh: [],
        kcdjb: [],
        sycp: [],
        sjdfp: [],
        sjdfp2: [],
        ksfxb: [],
        zpcjd: [],
        abj: [],
        da: [],
        dtj: []
      };
      this.readyForPrint = 1;
      this.$emit("printAllCheckted", this.printCallback);
    },
    printSingle: function() {
      console.log("singleCourseInfo =>", this.singleCourseInfo);
      this.singleCourseInfo.xueqi_1 = this.singleCourseInfo.xueqi.substr(0, 4);
      this.singleCourseInfo.xueqi_2 = this.singleCourseInfo.xueqi.substr(4);
      let printId = "none";
      let boolReadyForPrint = false;
      switch(this.printType) {
        case "sjcfm":
          // 试卷册封面
          printId = "print-test-book-cover-" + this.singleCourseInfo.id;
          boolReadyForPrint = true;
          break;
        case "kckh":
          // 课程考核实施计划
          printId = "print-assessment-implementation-plan-" + this.singleCourseInfo.id;
          boolReadyForPrint = true;
          break;
        case "mtjh":
          // 命题计划
          printId = "print-proposition-plan-" + this.singleCourseInfo.id;
          if (null != this.singleCourseInfo.planEntity) {
            boolReadyForPrint = true;
          } else {
            boolReadyForPrint = false;
          }
          break;
        case "kcdjb":
          // 考场登记表
          printId = "print-examination-room-registration-form-" + this.singleCourseInfo.id;
          boolReadyForPrint = true;
          break;
        case "sycp":
          // 送印传票
          printId = "print-delivery-of-summons-" + this.singleCourseInfo.id;
          if (null != this.singleCourseInfo.sendprinting) {
            boolReadyForPrint = true;
          } else {
            boolReadyForPrint = false;
          }
          break;
        case "sjdfp":
          // 试卷袋封皮
          printId = "print-test-paper-bag-cover-" + this.singleCourseInfo.id;
          if (null != this.singleCourseInfo.sexamination) {
            boolReadyForPrint = true;
          } else {
            boolReadyForPrint = false;
          }
          break;
        case "ksfxb":
          // 试卷分析表
          printId = "print-test-paper-analysis-form-" + this.singleCourseInfo.id;
          if (null != this.singleCourseInfo.shijuanfenxi) {
            boolReadyForPrint = true;
          } else {
            boolReadyForPrint = false;
          }
          break;
        case "zpcjd":
          // 总评成绩单
          printId = "print-general-evaluation-report-" + this.singleCourseInfo.id;
          boolReadyForPrint = true;
          break;
        default:
          break;
      }
      if (true === boolReadyForPrint) {
        printJS({
          printable: printId,
          type: 'html',
          targetStyles: ['*'],
          style: '@page { margin: 0mm 0mm; }'
        })
      }
    }
  },
  beforeUpdate: function() {
    // console.log("PrintSingle beforeUpdate componentType =>", this.componentType, "printType =>", this.printType, "singleCourseInfo =>", this.singleCourseInfo);
  },
  updated: function() {
    // console.log("PrintSingle updated componentType =>", this.componentType, "printType =>", this.printType, "singleCourseInfo =>", this.singleCourseInfo);
    if (2 == this.readyForPrint) {
      printJS({
        printable: "xpaas-print-all-selected-test-files",
        type: 'html',
        targetStyles: ['*'],
        style: '@page { margin: 0mm 0mm; }'
      });
      this.readyForPrint = 0;
    }
  },
  mounted: function() {
    // console.log("PrintSingle mounted componentType =>", this.componentType, "printType =>", this.printType, "singleCourseInfo =>", this.singleCourseInfo);
  }
}
</script>
